﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.DAL
{
    public class ExceptionMessageDAL
    {
        public static void Record(Exception ex)
        {
            Record("", ex);
        }

        public static void Record(string title, Exception ex)
        {
            var requestUrl = HttpContext.Current.Request.Url.PathAndQuery;
            var referrerUrl = HttpContext.Current.Request.UrlReferrer != null ? HttpContext.Current.Request.UrlReferrer.PathAndQuery : "";
            ErrorLogModel entity = new ErrorLogModel();
            entity.WebsiteName = HttpContext.Current.Request.Url.Host;
            entity.ReferrerUrl = referrerUrl;
            entity.RequestUrl = requestUrl;
            entity.Title = title;
            entity.Details = ex.ToString();


            Save(entity);
        }

        #region private const
        private const string PARAM_ID = "@Id";
        private const string PARAM_TITLE = "@Title";
        private const string PARAM_WEBSITE_NAME = "@WebsiteName";
        private const string PARAM_REQUEST_URL = "@RequestUrl";
        private const string PARAM_REFERRER_URL = "@ReferrerUrl";
        private const string PARAM_WEB_SERVER_IP = "@WebServerIp";
        private const string PARAM_CLIENT_IP = "@ClientIp";
        private const string PARAM_DETAILS = "@Details";
        private const string PARAM_CREATE_DATE = "@CreateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, ErrorLogModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.Id = reader.IsDBNull(reader.GetOrdinal("Id")) ? 0 : reader.GetInt32(reader.GetOrdinal("Id"));
                obj.Title = reader.IsDBNull(reader.GetOrdinal("Title")) ? String.Empty : reader.GetString(reader.GetOrdinal("Title"));
                obj.WebsiteName = reader.IsDBNull(reader.GetOrdinal("WebsiteName")) ? String.Empty : reader.GetString(reader.GetOrdinal("WebsiteName"));
                obj.RequestUrl = reader.IsDBNull(reader.GetOrdinal("RequestUrl")) ? String.Empty : reader.GetString(reader.GetOrdinal("RequestUrl"));
                obj.ReferrerUrl = reader.IsDBNull(reader.GetOrdinal("ReferrerUrl")) ? String.Empty : reader.GetString(reader.GetOrdinal("ReferrerUrl"));
                obj.WebServerIp = reader.IsDBNull(reader.GetOrdinal("WebServerIp")) ? 0 : reader.GetInt64(reader.GetOrdinal("WebServerIp"));
                obj.ClientIp = reader.IsDBNull(reader.GetOrdinal("ClientIp")) ? 0 : reader.GetInt64(reader.GetOrdinal("ClientIp"));
                obj.Details = reader.IsDBNull(reader.GetOrdinal("Details")) ? String.Empty : reader.GetString(reader.GetOrdinal("Details"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetErrorLogParams(ErrorLogModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_ID, SqlDbType.Int,0,obj.Id),
					 SQLHelper.MakeParam(PARAM_TITLE, SqlDbType.NVarChar, 1024,obj.Title),					
					 SQLHelper.MakeParam(PARAM_WEBSITE_NAME, SqlDbType.NVarChar, 1024,obj.WebsiteName),					
					 SQLHelper.MakeParam(PARAM_REQUEST_URL, SqlDbType.NVarChar, 2048,obj.RequestUrl),					
					 SQLHelper.MakeParam(PARAM_REFERRER_URL, SqlDbType.NVarChar, 2048,obj.ReferrerUrl),					
					 SQLHelper.MakeParam(PARAM_WEB_SERVER_IP, SqlDbType.BigInt,0,obj.WebServerIp),
					 SQLHelper.MakeParam(PARAM_CLIENT_IP, SqlDbType.BigInt,0,obj.ClientIp),
					 SQLHelper.MakeParam(PARAM_DETAILS, SqlDbType.NText, 0,obj.Details)				
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ErrorLogModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetErrorLogParams(obj);

                if (obj.Id == 0)
                {
                    obj.Id = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "ErrorLog_Insert", dbParams));
                    return obj.Id > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "ErrorLog_Insert", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                //Exception ex = new Exception("ErrorLog-->InsertOrUpdate-->" + e.Message);

                //ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }
    }
}
